<template>
  <div class="form-add2">
    <el-form :model="formObj" class="demo-form" ref="formTab" label-width="100px">
      <el-form-item label="电影标题">
         <el-input v-model="formObj.title" placeholder="电影标题" ></el-input>
      </el-form-item>
      <el-form-item label="主演">
        <el-input v-model="formObj.title" placeholder="请输入主演" ></el-input>
      
      </el-form-item>
     
      <el-form-item label="导演">
        <el-input v-model="formObj.director" placeholder="请输入导演" ></el-input>
      </el-form-item>
      <el-form-item label="主演图片合集">
       <UploadImg  @send="(data)=>getImageData(data,'imgSrc')" :imgHref="formObj.imgSrc"></UploadImg>
     </el-form-item>
      <el-form-item label="评分">
        <el-input v-model="formObj.score" placeholder="评分" ></el-input>
      </el-form-item>
      <el-form-item label="影片类型" >
        <el-checkbox-group v-model="formObj.movieType">
          <el-checkbox v-for="(item) in typeList" :key="item.id"  :label="item.id" > {{ item.name }}</el-checkbox>
        </el-checkbox-group>  
   
      </el-form-item>


      <el-form-item label="语言">
         <el-select v-model="formObj.language" placeholder="语言" >
          <el-option  v-for="item in languageList" :key="item._id" :label="item.language" :value="item.language"></el-option>
         </el-select>  
      </el-form-item>
   
      <el-form-item label="区域/放映时间">
         <ul class="region-box">
            <li v-for="(item,index) in formObj.releaseArr" :key="index"> 
              <el-select v-model="item.region" placeholder="所属区域" >
              <el-option  v-for="item in regionList" :key="item._id" :label="item.cname" :value="item._id"></el-option>
            </el-select>  
             

              <el-date-picker type="date" v-model="item.release" placeholder="放映时间"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              > </el-date-picker>
              <span class="btn-group" @click="manageRegionFun($event,index)">
                <i class="el-icon-circle-plus-outline" data-tag="add"></i>
                <i class="el-icon-remove-outline" data-tag="reduce" v-if="index!==0"></i>
              </span>
              
      
            </li>
         </ul>
      </el-form-item>

      <el-form-item label="描述">
        <el-input  type="textarea" v-model="formObj.desc" placeholder="描述" ></el-input>
      </el-form-item>

      <el-form-item label="电影海报">
       
        <UploadImg  @send="(data)=>getImageData(data,'posterSrc')"  :imgHref="formObj.posterSrc"></UploadImg>
     
      </el-form-item>
      <el-form-item label="宣传片">
        
        <UploadImg @send="(data)=>getImageData(data,'trailerLink')" :imgHref="formObj.trailerLink"></UploadImg>
      </el-form-item>

      <el-form-item>
            <el-button type="primary" @click="submitForm('formTab')">提交</el-button>
            <el-button @click="resetForm('formTab')">重置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import axiosEl from '@/utils/axios'
import UploadImg from '@/components/UploadImg.vue'

export default {
  data(){
    return {
      
     
      // releaseArr:[{region:'',release:''}],

      formObj:{
        releaseArr:[{region:'',release:''}],
        movieType:[],
        imgSrc:null,
        posterSrc:null,
        trailerLink:null
      },
      languageList:[],
      regionList:[],
      typeList: [],

      headerObj:{
           Authorization:'Bearer '
       },
    }
  },
  components:{
    UploadImg
  },
  methods:{

    getImageData(data,keyname,item){
      this.formObj[keyname] = data;
      item && (item.imgUrl = data);
    },

    onSuccessFun(response,kind){
      // response.data   图片上传成功后的名字
     this.formObj[kind] = response.data;
    },

   async addMovie(){
    let interUrl = this.formObj._id ? '/movie/update' : '/movie/add'
     let resData = await axiosEl({
        method:'post',
        url:interUrl,
        data:this.formObj
      })

      if(resData.code==1) {
        console.log(resData);
        
      }
    },
 
    getLanguage(){
      axiosEl.get('/language/all').then((resData)=>{
          if(resData.code==1) {
              this.languageList = resData.data;
          }
       })
    },
    getRegionList(){
      axiosEl.get('/region/all').then((resData)=>{
          if(resData.code==1) {
              this.regionList = resData.data;
          }
       })
    },
    getMovieType(){
      axiosEl.get('/type/all').then((resData)=>{
          if(resData.code==1) {
              this.typeList = resData.data;
          }
       })
    },
 
    manageRegionFun(ev,index){
        let tag = ev.target.dataset.tag;
        if(tag=='add') {
           this.formObj.releaseArr.push({region:'',release:''})
        } else {
          this.formObj.releaseArr.splice(index,1)
        }
    },
    // 获取电影详情信息
   async getMoieDetail(){
      if(!this.$route.query.id) { return; }
       let resDetail = await  axiosEl.get('/movie/detail/'+this.$route.query.id);
       console.log(resDetail);
       this.movieDetail = resDetail.data;
       
        this.formObj = resDetail.data;
    },
    submitForm(){

        this.addMovie();
    }
  },
  mounted(){
    let token = localStorage.getItem('token');
    this.headerObj = {
          Authorization:'Bearer '+token
    }

    this.getLanguage();
    this.getRegionList();
    this.getMovieType();
    this.getMoieDetail();
  }
}
</script>

<style lang="scss">
.form-add2 {
  width: 80%;
  input[type="text"] {
    width: 500px;
  }

  .web-img-box input[type="text"]{
    width: 100%;
  }
  .actor-box {
    input[type="text"] { width: 180px;}
  }

  .region-box {
    border: 1px dashed #666;
    padding: 10px;
  }
  .region-box li {
    display: flex;
    margin-bottom: 10px;
      .el-input {
        width: 200px;
        margin-right: 20px;
        input { width:100%; }
    }
  }
  .btn-group {
    font-size: 24px;
    i {
      margin-left: 10px;
      cursor: pointer;
      &:hover {
        color: #ff6600;
      }
    }
  }
}


</style>